class Admin::MilestonesController < AdminController
  def index
    @milestones = Milestone.all
  end

  def new
    @milestone = Milestone.new
  end

  def create
    @milestone = Milestone.new(params[:milestone])
   
    respond_to do |wants|
      if @milestone.save
        flash[:notice] = 'Model was successfully created.'
        wants.js{
          render :update do |page|
            page.replace_html 'new_milestone', :partial => "show", :locals => {:milestone => @milestone}
            page << "$j('#new_milestone').attr(\"id\",\"milestone_#{@milestone.id.to_s}\")"
            page << "$j('#new_milestone_action').before('<div id=\"new_milestone\"></div>')"
          end
        }
      else
        wants.js { render :partial => "show", :locals => {:milestone => @milestone} }
      end
    end 
  end

  def edit
    @milestone = Milestone.find(params[:id])
  end

  def update
    @milestone = Milestone.find(params[:id])
    respond_to do |wants|
      if @milestone.update_attributes(params[:milestone])
        wants.js{
          render :update do |page|
            page.replace_html 'milestone_' + @milestone.id.to_s, :partial => "show", :locals => {:milestone => @milestone}
          end
        }
      else
        wants.js { render :partial => "show", :locals => {:milestone => @milestone} } 
      end
    end
  end

  def destroy
    @milestone = Milestone.find(params[:id])
    p = @milestone.project
    @milestone.destroy
    flash[:notice] = "Successfully destroyed milestone."
    redirect_to edit_admin_project_url(p)
  end
  
  def show_remote_form
    @milestone  = (params[:id] == "new" ? Milestone.new : Milestone.find(params[:id]))
    @project    = (params[:id] == "new" ? Project.find(params[:project_id]) : @milestone.project)
    render :partial => "remote_form", :locals => {:milestone => @milestone}
  end
  
  def show
    @milestone = Milestone.find(params[:id])
    render :partial => "show", :locals => {:milestone => @milestone}
  end
end
